﻿using Idte.Common.Patterns;
using Idte.Common.Process;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Idte.Common.ArtifactModel
{
    public class Artifact : IComposite<KeyValuePair<string, object>>, IDictionary<string, object>
    {
        private readonly Dictionary<string, object> _innerDictionary = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
        private object data;
        public string Code { get; set; }
        public int? Sequence { get; set; }

        public ICollection<KeyValuePair<string, object>> Children { get; set; }
        public object Value
        {
            get { return data; }
            set
            {
                SetValue(value);
            }
        }

        //public ProcessDescriptor Process { get; set; }

        protected virtual void SetValue(object value)
        {
            data = value;
        }
        public void Add(string key, object value)
        {
            _innerDictionary.Add(key, value);
        }

        public bool ContainsKey(string key)
        {
            return _innerDictionary.ContainsKey(key);
        }

        public ICollection<string> Keys
        {
            get { return _innerDictionary.Keys; }
        }

        public bool Remove(string key)
        {
            return _innerDictionary.Remove(key);
        }

        public bool TryGetValue(string key, out object value)
        {
            return _innerDictionary.TryGetValue(key, out value);
        }

        public ICollection<object> Values
        {
            get { return _innerDictionary.Values; }
        }

        public object this[string key]
        {
            get
            {
                object value;
                _innerDictionary.TryGetValue(key, out value);
                return value;
            }
            set { _innerDictionary[key] = value; }
        }

        public void Add(KeyValuePair<string, object> item)
        {
            ((IDictionary<string, object>)_innerDictionary).Add(item);
        }

        public void Clear()
        {
            _innerDictionary.Clear();
        }

        public bool Contains(KeyValuePair<string, object> item)
        {
            return ((IDictionary<string, object>)_innerDictionary).Contains(item);
        }

        public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
        {
            ((IDictionary<string, object>)_innerDictionary).CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return _innerDictionary.Count; }
        }

        public bool IsReadOnly
        {
            get { return ((IDictionary<string, object>)_innerDictionary).IsReadOnly; }
        }

        public bool Remove(KeyValuePair<string, object> item)
        {
            return ((IDictionary<string, object>)_innerDictionary).Remove(item);
        }

        public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
        {
            return _innerDictionary.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return ((IEnumerable)_innerDictionary).GetEnumerator();
        }
    }
}
